#!/bin/sh
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $(lsb_release -cs) main restricted universe multiverse" > /etc/apt/sources.list 
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $(lsb_release -cs) main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $(lsb_release -cs)-updates main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $(lsb_release -cs)-updates main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $(lsb_release -cs)-backports main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $(lsb_release -cs)-backports main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $(lsb_release -cs)-security main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $(lsb_release -cs)-security main restricted universe multiverse" >> /etc/apt/sources.list


apt-get update

apt-get -y build-dep qemu

apt-get install -y build-essential apt-transport-https ca-certificates curl gnupg2 software-properties-common git python-pip libc++-dev libelf-dev libdwarf-dev libelf-dev libdwarf-dev libwiretap-dev wireshark-dev python-pycparser protobuf-compiler protobuf-c-compiler python-protobuf   libprotoc-dev libprotobuf-dev libprotobuf-c-dev unzip zip gcc-5 g++-5 libcapstone-dev libdwarf-dev python-pycparser chrpath  apt-utils

curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu  $(lsb_release -cs) stable"
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 50 && update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 50

apt-get install docker-ce docker-ce-cli containerd.io




/etc/docker/daemon.json

echo '{' > /etc/docker/daemon.json
echo '  "registry-mirrors" : [' >> /etc/docker/daemon.json
echo '    "http://ovfftd6p.mirror.aliyuncs.com",' >> /etc/docker/daemon.json
echo '    "http://registry.docker-cn.com",' >> /etc/docker/daemon.json
echo '    "http://docker.mirrors.ustc.edu.cn",' >> /etc/docker/daemon.json
echo '    "http://hub-mirror.c.163.com"' >> /etc/docker/daemon.json
echo '  ],' >> /etc/docker/daemon.json
echo '}' >> /etc/docker/daemon.json

service docker restart

mkdir /home/panda-libs
cd /home/panda-libs
git clone https://gitee.com/kingscat/panda_docker_env
cd panda_docker_env
mv ./* ../

git clone https://gitee.com/mirrors/capstone
cd capstone
./make.sh
./make.sh install
cd ..

tar zxvf libffi-3.0.13.tar.gz
cd libffi-3.0.13/
./configure --prefix=/usr --disable-static
make -j8
make install
cd ..

tar -xf llvm-3.3.src.tar.gz
cd llvm-3.3.src/
tar -xf ../cfe-3.3.src.tar.gz -C tools
mv tools/cfe-3.3.src tools/clang

sed -e 's:\$(PROJ_prefix)/docs/llvm:$(PROJ_prefix)/share/doc/llvm-3.3:' -i Makefile.config.in
CC=gcc CXX=g++ ./configure --prefix=/usr --sysconfdir=/etc --enable-libffi --enable-optimized --enable-shared --enable-targets=all --disable-assertions --disable-debug-runtime --disable-expensive-checks --enable-experimental-targets=R600
make -j16
make install 
cd ..

git clone https://github.com/panda-re/panda
mv ./panda /home/panda
cd /home/panda
mkdir build
cd build
../build.sh 

echo "export PANDA_BUILD=/home/panda/build" >> ~/.bashrc
echo "export PATH=$PATH:$PANDA_BUILD:$PANDA_BUILD/x86_64-softmmu:$PANDA_BUILD/i386-softmmu" >> ~/.bashrc
source ~/.bashrc

ln -s /home/panda/build/x86_64-softmmu/panda-system-x86_64 /usr/bin/panda64
ln -s /home/panda/build/i386-softmmu/panda-system-i386 /usr/bin/panda32


# simple
apt-get update && apt-get -y build-dep qemu && apt-get install -y build-essential apt-transport-https ca-certificates curl gnupg2 software-properties-common git python-pip libc++-dev libelf-dev libdwarf-dev libelf-dev libdwarf-dev libwiretap-dev wireshark-dev python-pycparser protobuf-compiler protobuf-c-compiler python-protobuf   libprotoc-dev libprotobuf-dev libprotobuf-c-dev unzip zip gcc-5 g++-5 libcapstone-dev libdwarf-dev python-pycparser chrpath  apt-utils && curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add - && sudo add-apt-repository "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu  $(lsb_release -cs) stable" && update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 50 && update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 50 && apt-get install docker-ce docker-ce-cli containerd.io && /etc/docker/daemon.json && echo '{' > /etc/docker/daemon.json && echo '  "registry-mirrors" : [' >> /etc/docker/daemon.json && echo '    "http://ovfftd6p.mirror.aliyuncs.com",' >> /etc/docker/daemon.json && echo '    "http://registry.docker-cn.com",' >> /etc/docker/daemon.json && echo '    "http://docker.mirrors.ustc.edu.cn",' >> /etc/docker/daemon.json && echo '    "http://hub-mirror.c.163.com"' >> /etc/docker/daemon.json && echo '  ],' >> /etc/docker/daemon.json && echo '}' >> /etc/docker/daemon.json && service docker restart && cd /home/panda-libs && git clone https://gitee.com/mirrors/capstone && cd capstone && ./make.sh && ./make.sh install && cd .. && tar zxvf libffi-3.0.13.tar.gz && cd libffi-3.0.13/ && ./configure --prefix=/usr --disable-static && make -j8 && make install && cd .. && tar -xf llvm-3.3.src.tar.gz && cd llvm-3.3.src/ && tar -xf ../cfe-3.3.src.tar.gz -C tools && mv tools/cfe-3.3.src tools/clang && sed -e 's:\$(PROJ_prefix)/docs/llvm:$(PROJ_prefix)/share/doc/llvm-3.3:' -i Makefile.config.in && CC=gcc CXX=g++ ./configure --prefix=/usr --sysconfdir=/etc --enable-libffi --enable-optimized --enable-shared --enable-targets=all --disable-assertions --disable-debug-runtime --disable-expensive-checks --enable-experimental-targets=R600 && make -j16 && make install  && cd .. && unzip panda.zip && mv ./panda /home/panda && cd /home/panda && mkdir build && cd build && ../build.sh  && echo "export PANDA_BUILD=/home/panda/build" >> ~/.bashrc && echo "export PATH=$PATH:$PANDA_BUILD:$PANDA_BUILD/x86_64-softmmu:$PANDA_BUILD/i386-softmmu" >> ~/.bashrc && source ~/.bashrc && ln -s /home/panda/build/x86_64-softmmu/panda-system-x86_64 /usr/bin/panda64 && ln -s /home/panda/build/i386-softmmu/panda-system-i386 /usr/bin/panda32